class DocumentSweeper < ActionController::Caching::Sweeper
  observe Document, Collection, Section

  def after_save(record)
    if record.is_a? Document
      expire_document(record)
    elsif record.is_a? Section
      record.self_and_descendants.each do |section|
        expire_documents_in_section(section)
      end
    elsif record.is_a? Collection
      record.sections.each do |section|
        expire_documents_in_section(section)
      end
    end
  end

  def expire_documents_in_section(section)
    section.documents.each do |document|
      expire_document(document)
    end
  end

  def expire_document(document)
    expire_page(:controller => "documents", :action => "show",
      :id => document.to_param)
  end
end

